package cn.edu.scau.cmi.yangqing.hibernate.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.yangqing.hibernate.dao.StudentDao;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.hibernate.util.HibernateUtil;

public class StudentService {
	
	StudentDao studentDao = new StudentDao();
	//添加学生
	public void addStudent(Student student) {
		Transaction beginTransaction1 = HibernateUtil.getCurrentSessionStu().beginTransaction();
		
		System.out.println("addStudent");
		
		try {
			studentDao.addStudent(student);
			
		} catch (Exception e) {
			System.out.println(e);
			beginTransaction1.rollback();
		}
		beginTransaction1.commit();
	}
	
	  //删除学生 
	public void deleteStudent(Integer id) { 
		Transaction beginTransaction2 = HibernateUtil.getCurrentSessionStu().beginTransaction();
		
		try { 
			studentDao.deleteStudent(id); 
		} catch (Exception e) {
			System.out.println(e);
			beginTransaction2.rollback(); 
		} 
		
		beginTransaction2.commit(); 
		
	}
	
	public Student findStudentById(Integer id) { 
		Transaction beginTransaction1 = HibernateUtil.getCurrentSessionStu().beginTransaction();
		StudentDao studentDao = new StudentDao(); 
		Student student = studentDao.findStudentById(id);
		beginTransaction1.commit();
		return student; 
	} 
	//修改学生 public void
	public void updateStudent(Student student) { 
		Transaction beginTransaction = HibernateUtil.getCurrentSessionStu().beginTransaction();
		
		StudentDao studentDao = new StudentDao();
		try { 
			studentDao.updateStudent(student); 
		} catch (Exception e) {
			beginTransaction.rollback();
		} 
		beginTransaction.commit();
	} 
	//显示所有学生 public
	public List<Student> findAllStudent() { 
		Transaction beginTransaction = HibernateUtil.getCurrentSessionStu().beginTransaction();	
	  StudentDao studentDao = new StudentDao();
	  List<Student> list = studentDao.findAllStudent();
	  beginTransaction.commit();
	  return list; 
	  }
	//通过导师id,选择其课程 
  	public void teachTutor(Integer tutorid,Integer id) { 
  		Transaction beginTransaction = HibernateUtil.getCurrentSessionStu().beginTransaction();
  		
  		StudentDao studentDao = new StudentDao(); 
  		try { 
  			studentDao.teachTutor(tutorid,id); 
  		} catch (Exception e){ 
  			beginTransaction.rollback();
  		} 
  		beginTransaction.commit(); 
  	}
	 
}
